package com.example.demo.controller;

import com.example.demo.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.example.demo.service.*;

@RestController
@RequestMapping("/api/blog-posts")
public class BlogPostController {

    @Autowired
    private BlogPostService blogPostService;

    //http://localhost:8080/api/blog-posts/create
    @PostMapping("/create")
    public ResponseEntity<BlogPost> createBlogPost(@RequestBody BlogPost blogPost) {
        BlogPost createdBlogPost = blogPostService.saveBlogPostWithAuthor(blogPost);
        return ResponseEntity.ok(createdBlogPost);
    }


    //http://localhost:8080/api/blog-posts/1
    @GetMapping("/{id}")
    public ResponseEntity<BlogPost> getBlogPostById(@PathVariable Long id) {
        return  blogPostService.getBlogPostById(id)
                .map(
                        blogPost -> ResponseEntity.ok().body(blogPost)
                ).orElse(
                        ResponseEntity.notFound().build()

                );
    }


}
